/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "碰撞.h"

//#include <几何图形/图形相交.h>





float32 f_graph_点与封套相交(const vec3& 点, const S_封套& 封套, const vec4& 起点平面) {
	float32 插值距离 = 0;

	float32 一半长度 = 封套.m_长度 * 0.5f;
	float32 相交宽度 = 一半长度 + DEF_Max(封套.m_封套S外半径, 封套.m_封套E外半径);

	vec3 中点 = 封套.m_端点 + 封套.m_方向 * 一半长度;
	if (vec_len(点 - 中点) < 相交宽度) {
		float32 ld = f_graph_点到直线距离(封套.m_端点, 封套.m_方向, 点);

		float32 到起点距离 = f_graph_点到平面距离(点, 起点平面);
		if (到起点距离 < 0) {
			插值距离 = vec_len(点 - 封套.m_端点);

			float32 区间距离 = 封套.m_封套S外半径 - 封套.m_封套S内半径;
			插值距离 = DEF_Max(插值距离 - 封套.m_封套S内半径, 0);
			插值距离 = DEF_Min(插值距离, 区间距离);
			插值距离 /= 区间距离;

			插值距离 = 1 - 插值距离;
		}
		else if (到起点距离 > 封套.m_长度) {
			插值距离 = vec_len(点 - (封套.m_端点 + 封套.m_方向* 封套.m_长度));
			
			float32 区间距离 = 封套.m_封套E外半径 - 封套.m_封套E内半径;
			插值距离 = DEF_Max(插值距离 - 封套.m_封套E内半径, 0);
			插值距离 = DEF_Min(插值距离, 区间距离);
			插值距离 /= 区间距离;

			插值距离 = 1 - 插值距离;
		}
		else {
			到起点距离 /= 封套.m_长度;
			float32 内圈距离 = mix(封套.m_封套S内半径, 封套.m_封套E内半径, 到起点距离);
			float32 外圈距离 = mix(封套.m_封套S外半径, 封套.m_封套E外半径, 到起点距离);

			float32 区间距离 = 外圈距离 - 内圈距离;

			插值距离 = DEF_Max(ld - 内圈距离, 0);
			插值距离 = DEF_Min(插值距离, 区间距离);
			插值距离 /= 区间距离;
			//插值距离 = 区间距离 / DEF_Max(ld - 内圈距离, 区间距离);
			插值距离 = 1 - 插值距离;
		}
		//float32 两端梯度 = 封套.m_封套E内半径 / 封套.m_封套S内半径;
	}
	else {
		插值距离 = 0;
	}

	return 插值距离;
}

bool f_collision_点是否在路径内(PolygonData& polygon) {
	
	uint32 num = polygon.edges.size();

	for (uint32 k = 0; k < num; ++k) {
		polygon.edges[k];

		for (uint32 i = 0; i < 5; ++i) {

		}
	}

    return false;
}








